package com.dominator.utils.qiniu;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Hex;

import com.dominator.utils.system.PropertiesLoader;

/**
 * 七牛时间戳防盗链签名
 * @author dl
 *
 */
public class SecurityTimestamp {
	
	public static PropertiesLoader propertiesLoader = new PropertiesLoader("sysconfig.properties");

	/**
     * 获取时间戳 URL签名
     * @param url
     * @param encryptKey
     * @param durationInSeconds
     * @return
     * @throws MalformedURLException
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    private static String getTimestamp(String url, String encryptKey, int durationInSeconds)
            throws MalformedURLException, UnsupportedEncodingException, NoSuchAlgorithmException {
        URL urlObj = new URL(url);
        String path = urlObj.getPath();

        long timestampNow = System.currentTimeMillis() / 1000 + durationInSeconds;
        String expireHex = Long.toHexString(timestampNow);

        String toSignStr = String.format("%s%s%s", encryptKey, path, expireHex);
        String signedStr = md5ToLower(toSignStr);

        String signedUrl = null;
        if (urlObj.getQuery() != null) {
            signedUrl = String.format("%s&sign=%s&t=%s", url, signedStr, expireHex);
        } else {
            signedUrl = String.format("%s?sign=%s&t=%s", url, signedStr, expireHex);
        }

        return signedUrl;
    }

    /**
     * MD5加密
     * @param src
     * @return
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    private static String md5ToLower(String src) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(src.getBytes("utf-8"));
        byte[] md5Bytes = digest.digest();
        return Hex.encodeHexString(md5Bytes);
    }

    
    /**
     * url生成防盗链地址
     * @param url
     */
    public static String urlAddSecurityTimestamp(String url){
    	 // cdn 配置的基于时间戳防盗链的加密字符串，cdn 配置完成后会得到
        String encryptKey =propertiesLoader.getProperty("qn_security_key") ;//加密key
        // 有效期
        int duration = propertiesLoader.getInteger("qn_security_duration");

        String signedUrl=null;
        try {
            signedUrl = getTimestamp(url, encryptKey, duration);
            System.out.println("加密成功"+signedUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            System.out.println("加密失败"+e.getMessage());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            System.out.println("加密失败"+e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            System.out.println("加密失败"+e.getMessage());
        }
        
		return signedUrl;
    }

    /**
     * Tester
     * @param args
     */
    public static void main(String args[]) {

      
        // 待加密链接
        String urlToSign = "http://video.jydsapp.com/video/1jyfjc1.mp4";
        
        
        urlAddSecurityTimestamp(urlToSign);
    }
}